import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/beans/video_content_bean.dart';
import 'package:hbtrans_flutter/utils/fluro.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';

import '../apis.dart';
import '../const.dart';
import '../routes.dart';

class VideoList extends StatefulWidget {
  final subjectId;

  const VideoList({Key key, this.subjectId}) : super(key: key);

  @override
  _VideoListState createState() => _VideoListState();
}

class _VideoListState extends State<VideoList> {
  List<VideoContentBean> items = List();

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10),
      child: _buildGrid(2, 1.6 / 1),
    );
  }

  _videoList() {
    Map data = Map<String, dynamic>();
    data["subjectId"] = widget.subjectId;
    MyDio.getInstance(url: Const.BASEURL).get(Apis.getVideoList, (data) {
      setState(() {
        for (var s in data) {
          items.add(VideoContentBean.fromJson(s));
        }
      });
    }, params: data);
  }

  @override
  void initState() {
    super.initState();
    _videoList();
  }

  _setNavigations() {
    return List.generate(
        items.length,
        (int index) => GestureDetector(
              onTap: () {
                var url = items[index].videopath;
                url = FluroConvertUtils.fluroCnParamsEncode(url);
                String route = '${Routes.myIjkPlayer}?url=$url';

                Routes.router.navigateTo(context, route,
                    replace: false, transition: TransitionType.fadeIn);
              },
              child: Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Image.network(
                      items[index].videocover,
                      width: 80 * 16 / 9,
                      fit: BoxFit.fill,
                      height: 80,
                    ),
                    SizedBox(
                      height: 2,
                    ),
                    Container(
                      width: 80 * 16 / 9,
                      height: 40,
                      child: Text(
                        items[index].videoname,
                        style: TextStyle(
                          fontSize: 13,
                          color: Colors.black,
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ));
  }

  _buildGrid(int i, var retio) {
    return GridView.count(
      // 禁止滚动
//      physics: new NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      childAspectRatio: retio,
      // 横轴数量
      crossAxisCount: i,
      padding: const EdgeInsets.all(0),
      // 主轴间隔
      mainAxisSpacing: 3,
      // 横轴间隔
      crossAxisSpacing: 2,
      children: _setNavigations(),
    );
  }
}
